package com.platform.api;

import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.LoginUser;
import com.platform.entity.OrderVo;
import com.platform.entity.ShopVo;
import com.platform.entity.TranferNewsVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiAccountService;
import com.platform.service.ApiOrderService;
import com.platform.service.ApiShopService;
import com.platform.util.ApiBaseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author zhanghao
 */
@Api(tags = "账户管理")
@RestController
@RequestMapping("/api/account")
@Slf4j
public class ApiAccountController extends ApiBaseAction {

    @Autowired
    private ApiAccountService accountService;
    @Autowired
    private ApiOrderService orderService;
    @Autowired
    private ApiShopService shopService;

    /**
     * 申请提现
     */
    @ApiOperation(value = "申请提现", response = Map.class)
    @PostMapping("applyTranfer")
    public Object applyTranfer(@LoginUser UserVo loginUser) {
        JSONObject json = this.getJsonRequest();
        TranferNewsVo entity = JSONObject.toJavaObject(json,TranferNewsVo.class);
        String order_sn = entity.getOrder_sn();

        //判断该订单是否已提交申请
        List<TranferNewsVo> vos = accountService.querytranferByOrderSn(order_sn);
        if (vos.size()>0){
            return toResponsFail("该订单已提交过提现申请");
        }

        //根据订单号查询订单信息
        OrderVo orderVo = orderService.queryObjectByOrderSn(order_sn);
        //查询店铺信息
        ShopVo shopVo = shopService.queryByUserId(loginUser.getUserId().intValue());

        //封装数据
        entity.setUserId(loginUser.getUserId().intValue());
        entity.setShopId(shopVo.getShopId());
        entity.setTranferMoney(orderVo.getOrder_price().toString());
        entity.setStatus(2);

        log.info("申请体现数据为：{}", JSONObject.toJSONString(entity));
        int saveFlag = accountService.applyTranfer(entity);
        if (saveFlag == 1){
            return toResponsSuccess(null);
        }else {
            return toResponsFail(null);
        }
    }

    /**
     * 查询提现记录
     */
    /*@ApiOperation(value = "查询提现记录", response = Map.class)
    @PostMapping("tranferNews")
    @IgnoreAuth
    public Object querytranferNewsList() {

        return toResponsSuccess(accountService.querytranferNewsList());
    }*/

}